package com.taskcollection.util;


import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;
import java.sql.SQLException;


/**
 * Created by Administrator on 2016/7/17 0017.
 */
public class DBConnection {
    private static DBConnection du=null;
    private DruidDataSource dataSource;
    private DBConnection(){
        dataSource= new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://127.0.0.1:3306/hao?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull";
        String username="root";
        String password="123456";
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setInitialSize(5);
        dataSource.setMaxActive(20);
        dataSource.setMinIdle(1);
    }

    public static  DBConnection getinstance(){
        if(du==null){
            du=new DBConnection();
        }
        return du;
    }
    public Connection getConnection(){
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            conn=null;
        }
        try {
            System.out.println("************************"+conn.isClosed());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  conn;
    }
}
